package com.ulic.customerManApplication.messagemodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.commonmodule.bo.BirthdayMessage;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.messagemodule.bo.MessageRecord;
import com.ulic.customerManApplication.messagemodule.dao.MessageDao;
import com.ulic.customerManApplication.messagemodule.vo.MessageRecordVO;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;
@Component("messageDao")
public class MessageDaoImpl extends BaseDao implements MessageDao {

	Session session = null;
	@Override
	public void deleteMessageRecord(MessageRecord messageRecord) {

		session = hibernateTemplate.getSessionFactory().getCurrentSession();
		session.delete(messageRecord);
		session.flush();
	}

	@SuppressWarnings("unchecked")
	@Override
	public MessageRecord getMessageRecordById(int id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(MessageRecord.class);
		List<MessageRecord> list = cri.add(Restrictions.eq("message_id", id)).list();
		MessageRecord messageRecord = null;
		if(list.size()>0){
			messageRecord	= (MessageRecord)list.get(list.size()-1);
		}
		return messageRecord;
	}

	@Override
	public PagerModel getMessageRecords(int curPage) {
		String hql = "from MessageRecord messageRecord order by sendTime desc";
		return searchPaginated(hql, curPage, CONSTANT.PageSize);
	}

	@Override
	public PagerModel getMessageRecords(MessageRecordVO messageRecordVO,
			int curPage) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from MessageRecord messageRecord where messageRecord.sender.um_user_id ="+messageRecordVO.getUser().getUm_user_id());
		if(messageRecordVO != null){
			if(messageRecordVO.getStartDate() != null && messageRecordVO.getStartDate().length() != 0){
				sb.append(" and to_date(messageRecord.sendTime,'YYYY-MM-DD  HH24:MI:SS')>= to_date('"+messageRecordVO.getStartDate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(messageRecordVO.getEndDate() != null && messageRecordVO.getEndDate().length() != 0){
				sb.append(" and to_date(messageRecord.sendTime,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+messageRecordVO.getEndDate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by sendTime desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}

	@Override
	public void insertBirthdayBless(BirthdayMessage birthdayMessage) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.saveOrUpdate(birthdayMessage);

	}

	@Override
	public void insertMessageRecords(MessageRecord messageRecord) {
		session = hibernateTemplate.getSessionFactory().getCurrentSession();
		try{
			session.save(messageRecord);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.flush();
	}

	@SuppressWarnings("unchecked")
	@Override
	public BirthdayMessage getBirthdayMessageByType(User user,String type) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(BirthdayMessage.class);
		List<BirthdayMessage> list = cri.add(Restrictions.eq("user.um_user_id", user.getUm_user_id())).add(Restrictions.eq("type", type)).addOrder(Order.desc("id")).list();
		BirthdayMessage birthdayMessage = null;
		if(list.size()>0){
			birthdayMessage	= (BirthdayMessage)list.get(0);
		}
		return birthdayMessage;
	}

}
